//
//  StatusCell.swift
//  Weibo8023
//
//  Created by styshy on 15/12/11.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit
import FFLabel

let StatusCellMargin:CGFloat = 12
let StatusCellIconWidth:CGFloat = 35

class StatusCell: UITableViewCell {
    
    var viewModel:StatusViewModel?{
        
        didSet{
            topView.viewModel = viewModel
            
            let text = viewModel?.status.text ?? ""
            contentLabel.attributedText = EmoticonManager.sharedManager.emoticonText(text, font: contentLabel.font)
            
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                // 直接设置宽度数值
                make.width.equalTo(pictureView.bounds.width)
            }
        }
    }
    
    func rowHeight(vm:StatusViewModel) -> CGFloat{
        viewModel = vm
        contentView.layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    
    
    // MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        // TODO:设置选中
        selectionStyle = .None
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 懒加载控件
    private lazy var topView:StatusCellTopView = StatusCellTopView()
    
    /// 微博正文内容
    lazy var contentLabel:FFLabel = FFLabel(title: "微博正文", fontSize: 15, color: UIColor.darkGrayColor(), screenInset: StatusCellMargin)
    
    /// 设置图片内容
    lazy var pictureView:StatusPictureView = StatusPictureView()
    
    /// 微博底部内容
    lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    
    // 定义一个代理
    weak var labelDelegate:StatusCellDelegate?
}

protocol StatusCellDelegate:NSObjectProtocol{
    func statusCellDidClickLabelLinked(text:String)
}

// MARK: - FFLabelDelegate 代理
extension StatusCell:FFLabelDelegate{
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        labelDelegate?.statusCellDidClickLabelLinked(text)
    }
}

extension StatusCell{
    func setupUI(){
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        // 2. 自动布局
        // 1> 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            
        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
        
        
        contentLabel.labelDelegate = self
    }
}
